#include "mainloginwidget.h"
#include "phoneloginwidget.h"
#include "utils/util.h"
#include <Functionality/applycertificatefactory.h>
#include <Functionality/functionalityFactory.h>
#include <utils/keymanager.h>
#include <utils/signalcenter.h>
mainLoginWidget::mainLoginWidget(QWidget *parent)
    : QWidget{parent}
    , widgetStack(new QStackedWidget(this))
    , loginCodeWidget(new loginCode(this))
    , titleBarHLayout(new QHBoxLayout())
    , mainVLayout(new QVBoxLayout(this))
    , label(new QLabel())
    , btnMin(new QPushButton())
    , btnClose(new QPushButton())
    , phonelogin(new phoneLoginWidget(this))
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    widgetStack->addWidget(loginCodeWidget);

    setMinimumSize(QSize(420, 700));
    setMaximumSize(QSize(420, 700));
    titleBarHLayout->setSpacing(6); //设置子控件的间距
    titleBarHLayout->setObjectName("titleBarHLayoout");
    titleBarHLayout->setAlignment(Qt::AlignCenter);
    //这是设置标题
    setLable(label, "label", "爱科会议", "Microsoft YaHei", 10);
    QHBoxLayout *titleHLayout = new QHBoxLayout();
    label->setMinimumSize(QSize(390, 30));
    label->setMaximumSize(QSize(390, 30));
    label->setAlignment(Qt::AlignCenter);
    titleHLayout->addWidget(label);

    titleBarHLayout->addLayout(titleHLayout);

    setPushBtn(btnMin,
               "btnMin",
               QSize(25, 30),
               "",
               "QPushButton{border:none}",
               true,
               ":/resources/min1.png");

    setPushBtn(btnClose,
               "btnClose",
               QSize(25, 30),
               "",
               "QPushButton{border:none}",
               true,
               ":/resources/close1.png");

    addButtonIconMapping(btnMin, ":/resources/min2.png", ":/resources/min1.png");
    addButtonIconMapping(btnClose, ":/resources/close2.png", ":/resources/close1.png");
    QHBoxLayout *minCloseBtnHLayout = new QHBoxLayout();
    minCloseBtnHLayout->addWidget(btnMin);
    minCloseBtnHLayout->addWidget(btnClose);
    titleBarHLayout->addLayout(minCloseBtnHLayout);
    // mainVLayout->setAlignment(Qt::AlignCenter);
    mainVLayout->addLayout(titleBarHLayout);
    mainVLayout->addWidget(widgetStack);
    widgetStack->setCurrentIndex(0);

    //接着就是添加phonelogin
    phonelogin->setObjectName("phonelogin");
    widgetStack->addWidget(phonelogin);

    checkAndInitializeCertificates();
    connect(btnMin, &QPushButton::clicked, this, [=]() { showMinimized(); });
    connect(btnClose, &QPushButton::clicked, this, [=]() {
        emit SIGNALCENTER.closeWidget();
        close();
    });

    connect(&SIGNALCENTER, &SignalCenter::toolBtnPhoneClicked, this, [&]() {
        widgetStack->setCurrentIndex(PHONE_LOGIN_INDEX);
    });

    connect(phonelogin, &phoneLoginWidget::returnMainLoginWidget, this, [&]() {
        widgetStack->setCurrentIndex(MAIN_LOGIN_INDEX);
    });
}

void mainLoginWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_dragging = true;
        m_offset = event->globalPosition().toPoint() - frameGeometry().topLeft();
        event->accept();
    }
    // event->globalPosition().toPoint() 这里是获得当前鼠标的位置
    //frameGeometry().topLeft() 中frameGeometry()是获得窗体的外部框架信息，包括位置信息
    //topLeft()是获得左上角的位置信息
    //因此
}

void mainLoginWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (m_dragging) {
        move(QPoint(static_cast<int>(event->globalPosition().x() - m_offset.x()),
                    static_cast<int>(event->globalPosition().y() - m_offset.y())));
        event->accept();
    }
}

void mainLoginWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_dragging = false;
        event->accept();
    }
}

void mainLoginWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //设置抗锯齿的渲染属性
    painter.setRenderHint(QPainter::Antialiasing);

    //设置它的内部填充颜色是白色
    painter.setBrush(QColor(255, 255, 255));

    painter.setPen(Qt::gray);

    //获取绘画的区域
    QRect rect = this->rect();
    painter.drawRoundedRect(rect, 10, 10);
    QWidget::paintEvent(event);
}

void mainLoginWidget::addButtonIconMapping(QAbstractButton *button,
                                           const QString &iconEnter,
                                           const QString &iconLevel)
{
    if (button) {
        buttonIconMap.insert(button, QPair<QString, QString>(iconEnter, iconLevel));
        button->installEventFilter(this);
    }
}

bool mainLoginWidget::eventFilter(QObject *watch, QEvent *event)
{
    QAbstractButton *button = qobject_cast<QAbstractButton *>(watch);
    if (button && buttonIconMap.contains(button)) {
        const QPair<QString, QString> &icons = buttonIconMap.value(button);
        if (event->type() == QEvent::Enter) {
            changeIcon(button, icons.first);
        } else if (event->type() == QEvent::Leave) {
            changeIcon(button, icons.second);
        }
    }
    return QObject::eventFilter(watch, event);
}

void mainLoginWidget::checkAndInitializeCertificates()
{
    // qDebug() << "mainLoginWidget";
    bool ret = keyManager::instance().initializeKeys();
    //说明本地公钥证书和私钥已经失效了，就需要重新创建公私钥和申请公钥证书
    //创建申请证书功能
    if (!ret) {
        factory = std::make_shared<applyCertificateFactory>();
        Functionality = factory->create();
        if (!Functionality->executeFunction()) {
            std::cerr << "fail to executeFunction " << __LINE__ << std::endl;
            return;
        }

        //executeFunction后续还没写完的。因为还有后续拿到数据
    }
}
